/** 
 * 日期比较大小 
 * compareDateString大于dateString，返回1； 
 * 等于返回0； 
 * compareDateString小于dateString，返回-1 
 * @param {日期字符串} dateString
 * @param {比较的日期} compareDateString  
 */
// export function dateCompare(dateString, compareDateString) {
//     var dateTime = dateParse(dateString).getTime();
//     var compareDateTime = dateParse(compareDateString).getTime();
//     if (compareDateTime > dateTime) {
//       return true;
//     } else if (compareDateTime == dateTime) {
//       return false;
//     } else {
//       return false;
//     }
//   };
export function dateCompare(dateString) {
  const dateTime = dateParse(dateString).getTime();

  // 减去24小时
  const compareDateTime = new Date().getTime() - 24 * 60 * 60 * 1000;
  if (compareDateTime > dateTime) {
    return true;
  } if (compareDateTime === dateTime) {
    return false;
  }
  return false;
}

/**
 * 将日期字符串转换为Date
 * @param {日期字符串} dateString 
 */
export function dateParse(dateString) {
  var SEPARATOR_BAR = "-";
  var SEPARATOR_SLASH = "/";
  var SEPARATOR_DOT = ".";
  var dateArray;
  if (dateString.indexOf(SEPARATOR_BAR) > -1) {
    dateArray = dateString.split(SEPARATOR_BAR);
  } else if (dateString.indexOf(SEPARATOR_SLASH) > -1) {
    dateArray = dateString.split(SEPARATOR_SLASH);
  } else {
    dateArray = dateString.split(SEPARATOR_DOT);
  }
  return new Date(dateArray[0], dateArray[1] - 1, dateArray[2]);
};

// 获取当前时间戳
export function getCurrentTimestamp() {
  return new Date().getTime();
}

export const newDateTransform = (timedata, t) => {
  let target = t;
  const date = new Date(timedata.substr(0, 19));
  const currentTime = date.getTime();
  const offset = date.getTimezoneOffset() * 60000;
  const utcTime = currentTime + offset;
  const newDate = new Date(utcTime + target * 3600000);
  const Year = newDate.getFullYear();
  const Month = newDate.getMonth() + 1 < 10
    ? `0${newDate.getMonth() + 1}`
    : newDate.getMonth() + 1;
  const d = newDate.getDate() < 10 ? `0${newDate.getDate()}` : newDate.getDate();
  const Hours = newDate.getHours() < 10 ? `0${newDate.getHours()}` : newDate.getHours();
  const Minutes = newDate.getMinutes() < 10
    ? `0${newDate.getMinutes()}`
    : newDate.getMinutes();
  const Seconds = newDate.getSeconds() < 10
    ? `0${newDate.getSeconds()}`
    : newDate.getSeconds();
  const overTime = `${Year}/${Month}/${d} ${Hours}:${Minutes}:${Seconds}`;
  return overTime;
};

// 获取当前年月日时分秒
export function getCurrentDate() {
  let d = new Date()
  let year = d.getFullYear()
  let month = d.getMonth()
  month = month + 1 > 12 ? 1 : month + 1
  month = month > 9 ? month : '0' + month.toString()
  let day = d.getDate()
  let hour = d.getHours()
  hour = hour > 9 ? hour : '0' + hour.toString()
  let minute = d.getMinutes()
  minute = minute > 9 ? minute : '0' + minute.toString()
  let second = d.getSeconds()
  return `${year}-${month}-${day} ${hour}:${minute}:${second}`
}